#include common_scripts\utility;
#include maps\mp\_utility;
#include maps\mp\gametypes\_hud_util;
#include z_mod\_utility;

init()
{
    level.z_Souls = [];
    
    level.z_Souls[0] = spawnStruct();
    level.z_Souls[0].energy = 100;
    level.z_Souls[0].hp = 110;
    level.z_Souls[0].img = "cardtitle_skateboard";
    level.z_Souls[0].name = "Decathlete";
    level.z_Souls[0].player = undefined;
    level.z_Souls[0].skills = [];
    level.z_Souls[0].skills[0] = createSoulSkill(::skill_decathlete_1, "cardicon_boots_01"); // пассивный
    level.z_Souls[0].skills[1] = createSoulSkill(::skill_decathlete_2, "cardicon_booniehat"); // активный
    level.z_Souls[0].skills[2] = createSoulSkill(::skill_decathlete_3, "cardicon_boot"); // аура
    level.z_Souls[0].skills[3] = createSoulSkill(::skill_none, "cardicon_locked");
    level.z_Souls[0].skin = "smg";
    level.z_Souls[0].speed = 1.1;
    level.z_Souls[0].weapon = "kriss_mp";
    
    level.z_Souls[1] = spawnStruct();
    level.z_Souls[1].energy = 100;
    level.z_Souls[1].hp = 90;
    level.z_Souls[1].img = "cardtitle_sniper_4";
    level.z_Souls[1].name = "Sniper";
    level.z_Souls[1].player = undefined;
    level.z_Souls[1].skills = [];
    level.z_Souls[1].skills[0] = createSoulSkill(::skill_none, "cardicon_locked");
    level.z_Souls[1].skills[1] = createSoulSkill(::skill_none, "cardicon_locked");
    level.z_Souls[1].skills[2] = createSoulSkill(::skill_none, "cardicon_locked");
    level.z_Souls[1].skills[3] = createSoulSkill(::skill_none, "cardicon_locked");
    level.z_Souls[1].skin = "camo";
    level.z_Souls[1].speed = 0.95;
    level.z_Souls[1].weapon = "cheytac_mp";
    
    
    for(i = 0; i < level.z_Souls.size; i++) {
        precacheShader(level.z_Souls[i].img);
    }
    
    
    level.z_SoulsExp = [];
    level.z_SoulsExp[0] = 0;
    level.z_SoulsExp[1] = 0;
    level.z_SoulsExp[2] = 100;
    level.z_SoulsExp[3] = 200;
    level.z_SoulsExp[4] = 300;
    level.z_SoulsExp[5] = 400;
    level.z_SoulsExp[6] = 500;
    level.z_SoulsExp[7] = 600;
    level.z_SoulsExp[8] = 700;
    level.z_SoulsExp[9] = 800;
    level.z_SoulsExp[10] = 900;
    level.z_SoulsExp[11] = 1000;
    level.z_SoulsExp[12] = 1100;
    level.z_SoulsExp[13] = 1200;
    level.z_SoulsExp[14] = 1300;
    level.z_SoulsExp[15] = 1400;
    level.z_SoulsExp[16] = 1500;
    level.z_SoulsExp[17] = 1600;
    level.z_SoulsExp[18] = 1700;
    level.z_SoulsExp[19] = 1850;
    level.z_SoulsExp[20] = 2000;
}

createSoulSkill(func, icon)
{
    skill = spawnStruct();
    
    skill.func = func;
    skill.icon = icon;
    
    precacheShader(icon);
    
    return skill;
}

getRandomSoul()
{
    available_souls = [];
    ignore_busy = getDvarInt("z_UniqueSouls") > 0;
    
    for(i = 0; i < level.z_Souls.size; i++) {
        if(ignore_busy && isDefined(level.z_Souls[i].player))
            continue;
        
        available_souls[available_souls.size] = i;
    }
    
    return available_souls[randomInt(available_souls.size)];
}

getSoulLvl()
{
    lvl = 0;
    
    while(level.z_SoulsExp[lvl] <= self.z_SoulExp && lvl < level.z_SoulsExp.size)
        lvl++;
    
    return lvl;
}

giveEmptySoul()
{
    self takeAllWeapons();
    
    self.specialty = [];
    
    self.class_num = 0;
    self.curClass = "custom1";
    
    self.loadoutPrimary = "none";
    self.loadoutPrimaryCamo = 0;
    self.loadoutSecondary = "none";
    self.loadoutSecondaryCamo = 0;
    
    self setOffhandPrimaryClass("other");
    
    self _SetActionSlot(1, "");
    //self _SetActionSlot(2, "nightvision");
    self _SetActionSlot(3, "altMode");
    self _SetActionSlot(4, "");

    self _clearPerks();
    self maps\mp\gametypes\_class::_detachAll();
    
    self maps\mp\gametypes\_class::setKillstreaks("none", "none", "none");
    
    self.pers["primaryWeapon"] = "none";
    
    self setOffhandSecondaryClass("flash");

    self.primaryWeapon = "none";
    self.secondaryWeapon = "none";

    self maps\mp\gametypes\_teams::playerCustomModel("");

    self.isSniper = false;

    self maps\mp\gametypes\_weapons::customUpdateMoveSpeedScale(1.0);

    //self maps\mp\perks\_perks::cac_selector();
    
    self notify("changed_kit");
    self notify("giveLoadout");
}

giveSoul()
{
    self takeAllWeapons();
    
    self.specialty = [];
    
    self.class_num = 0;
    self.curClass = "custom1";
    
    self.loadoutPrimary = "none";
    self.loadoutPrimaryCamo = 0;
    self.loadoutSecondary = "none";
    self.loadoutSecondaryCamo = 0;
    
    self setOffhandPrimaryClass("other");
    
    self _SetActionSlot(1, "");
    //self _SetActionSlot(2, "nightvision");
    self _SetActionSlot(3, "altMode");
    self _SetActionSlot(4, "");

    self _clearPerks();
    self maps\mp\gametypes\_class::_detachAll();
    
    self maps\mp\gametypes\_class::setKillstreaks("none", "none", "none");
    
    self.pers["primaryWeapon"] = "none";
    
    self setOffhandSecondaryClass("flash");

    self.primaryWeapon = "none";
    self.secondaryWeapon = "none";

    self maps\mp\gametypes\_teams::playerCustomModel(level.z_Souls[self.z_SoulCurrent].skin);

    self.isSniper = false;

    self maps\mp\gametypes\_weapons::customUpdateMoveSpeedScale(level.z_Souls[self.z_SoulCurrent].speed);

    //self maps\mp\perks\_perks::cac_selector();
    
    self.health = level.z_Souls[self.z_SoulCurrent].hp;
    self.maxhealth = level.z_Souls[self.z_SoulCurrent].hp;
    
    self giveWeapon(level.z_Souls[self.z_SoulCurrent].weapon, 0, isSubStr(level.z_Souls[self.z_SoulCurrent].weapon, "akimbo"));
    self giveMaxAmmo(level.z_Souls[self.z_SoulCurrent].weapon);
    
    self setSpawnWeapon(level.z_Souls[self.z_SoulCurrent].weapon);
    
    for(i = 0; i < level.z_Souls[self.z_SoulCurrent].skills.size; i++) {
        self thread [[ level.z_Souls[self.z_SoulCurrent].skills[i].func ]]();
        
        self z_mod\_hud::showHUD_Skills();
        
        self.z_HUDSkills[i].shader = level.z_Souls[self.z_SoulWanted].skills[i].icon;
        self.z_HUDSkills[i] setShader(self.z_HUDSkills[i].shader, 24, 24);
    }
    
    self notify("changed_kit");
    self notify("giveLoadout");
}

skill_decathlete_1()
{
    //self endon("death");
    //self endon("disconnect");
    
    self maps\mp\perks\_perks::givePerk("specialty_marathon");
    self maps\mp\perks\_perks::givePerk("specialty_falldamage");
}

skill_decathlete_2()
{
    self endon("death");
    self endon("disconnect");
    
    while(true) {
        self waittill("zkey_actionslot_2");
        
        if(self.z_Energy < 60)
            continue;
        
        old_velocity = self getVelocity();
        new_velocity = (old_velocity[0] * 5.0, old_velocity[1] * 5.0, old_velocity[2] * 5.0);
        
        self setVelocity(new_velocity);
        
        self.z_Energy -= 60;
    }
}

skill_decathlete_3()
{
    self endon("death");
    self endon("disconnect");
    
    new_speed = level.z_Souls[self.z_SoulCurrent].speed * 1.5;
    
    while(true) {
        self waittill("zkey_actionslot_3");
        
        if(self.z_Energy < 8)
            continue;
        
        self maps\mp\gametypes\_weapons::customUpdateMoveSpeedScale(new_speed);
        self allowJump(false);
        
        self.z_Energy -= 8;
        
        while(self.z_Energy > 0) {
            act = self waittill_any_timeout(1.0, "zkey_actionslot_3");
            
            if(act == "timeout") {
                if(self.z_Energy < 8)
                    self.z_Energy = 0;
                else
                    self.z_Energy -= 8;
            } else if(act == "zkey_actionslot_3") {
                break;
            }
        }
        
        self maps\mp\gametypes\_weapons::customUpdateMoveSpeedScale(level.z_Souls[self.z_SoulCurrent].speed);
        self allowJump(true);
    }
}

skill_none()
{
}

